package edu.beyond.employment.company.dao;

import java.util.ArrayList;
import java.util.List;

import edu.beyond.employment.commom.util.DBUtil;
import edu.beyond.employment.company.entity.PositionEntity;

/**
 * POsitionDao层
 * 
 * @author GOBeyond
 * @date 2016年9月28日
 */
public class PositionDao {
	/**
	 * 首页所有职位的列举
	 * 
	 * @return
	 */
	public List<PositionEntity> list() {
		String sql = "SELECT c.name as 'company$name', c.address as 'company$address', c.slogan as 'company$slogan', p.id, p.name, p.salary1, p.salary2, p.release_time FROM company c, position p WHERE c.id=p.company_id ORDER BY p.id DESC ";
		List<PositionEntity> positionList = DBUtil.executeQuery(PositionEntity.class, sql);
		return positionList;
	}

	/**
	 * 根据id查询赤尾职位信息详情
	 * 
	 * @param id
	 * @return
	 */
	public PositionEntity detail(int id) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT ");
		sb.append("c.name as 'company$name', ");
		sb.append("c.email as 'company$email', ");
		sb.append("c.phone as 'company$phone', ");
		sb.append("c.linkman as 'company$linkman', ");
		sb.append("c.address as 'company$address', ");
		sb.append("c.numbers as 'company$numbers', ");
		sb.append("c.slogan as 'company$slogan', ");
		sb.append("p.id, ");
		sb.append("p.name, ");
		sb.append("p.duty, ");
		sb.append("p.ability, ");
		sb.append("p.salary1, ");
		sb.append("p.salary2, ");
		sb.append("p.release_time ");
		sb.append("FROM company c, position p ");
		sb.append("WHERE c.id=p.company_id AND p.id=? ");
		return DBUtil.getUniqueResult(PositionEntity.class, sb.toString(), id);
	}

	/**
	 * 发布职位
	 * 
	 * @param position
	 * @return
	 */
	public int add(PositionEntity position) {
		List<Object> params = new ArrayList<>();
		params.add(position.getCompany$id());
		params.add(position.getName());
		params.add(position.getDuty());
		params.add(position.getAbility());
		params.add(position.getSalary1());
		params.add(position.getSalary2());
		int row = DBUtil.executeUpdate("INSERT INTO position VALUES(NULL ,?,?,?,?,?,?,NOW())", params);
		return row;
	}

	/**
	 * 删除职位
	 * 
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		return DBUtil.executeUpdate("DELETE FROM position WHERE id=?", id);
	}

	/**
	 * 职位详情信息查询
	 * 
	 * @param id
	 * @return
	 */
	public PositionEntity get(int id) {
		String sql = "SELECT * FROM position WHERE id=?";
		return DBUtil.getUniqueResult(PositionEntity.class, sql, id);

	}

	/**
	 * 查询当前登录企业的所有发布的职位信息
	 * 
	 * @param companyId
	 * @return
	 */
	public List<PositionEntity> listById(int companyId) {
		List<PositionEntity> positionList = DBUtil.executeQuery(PositionEntity.class,
				"SELECT * FROM position WHERE company_id=? ORDER BY id DESC", companyId);
		return positionList;
	}

	public int update(PositionEntity position) {
		// TODO Auto-generated method stub
		return 0;
	}
}
